#pragma once
#include <iostream>

template <class T>
class ShareVector {
public:
	ShareVector(bool block = true);
	~ShareVector();
	bool add(T data);
	bool erase(int pos);
	void lock();
	void unLock();
	T get(int pos);
	int size();
private:
	std::vector<T> vector;
	std::mutex mutex;
	std::condition_variable cond_variable;
	int count = 0
	bool block = false;
	bool canRead = true;
};

template<class T>
inline ShareVector<T>::ShareVector(bool block)
{
	this->block = false;
}

template<class T>
inline ShareVector<T>::~ShareVector()
{
}

template<class T>
inline bool ShareVector<T>::add(T data)
{
	std::unique_lock<std::mutex> lock(mutex);
	this->vector.emplace_back(data);
	return true;
}

template<class T>
inline bool ShareVector<T>::erase(int pos)
{
	std::unique_lock<std::mutex> lock(mutex);
	this->vector.erase(pos);
	return false;
}

template<class T>
inline void ShareVector<T>::lock()
{
	std::unique_lock<std::mutex> lock(mutex);
	this->canRead = false;
}

template<class T>
inline void ShareVector<T>::unLock()
{
	std::unique_lock<std::mutex> lock(mutex);
	this->canRead = true;
	this->cond_variable.notifyAll();
}

template<class T>
inline T ShareVector<T>::get(int pos)
{
	std::unique_lock<std::mutex> lock(mutex);
	while (!this->canRead) {
		this->cond_variable.wait(lock);
	}
	T data = this->vector.at(pos);
	return data;
}

template<class T>
inline int ShareVector<T>::size()
{
	std::unique_lock<std::mutex> lock(mutex);
	while (!this->canRead) {
		this->cond_variable.wait(lock);
	}
	return this->vector.size();
}
